package com.dengqi.mymodule.activity.update;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import java.io.File;

import androidx.core.content.FileProvider;

import static android.content.Context.MODE_PRIVATE;

public class DownloadReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            installApk(context, id);
        } else if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
            // DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            //获取所有下载任务Ids组
            //long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
            ////点击通知栏取消所有下载
            //manager.remove(ids);
            //Toast.makeText(context, "下载任务已取消", Toast.LENGTH_SHORT).show();
            //处理 如果还未完成下载，用户点击Notification ，跳转到下载中心
//            Intent viewDownloadIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
//            viewDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(viewDownloadIntent);
        } else if (intent.getAction().equals("my.action.stallapk")) {
            String apk_path = intent.getStringExtra("apk_path");
            File apkFile = new File(apk_path);
            installApk(context, apkFile);
        }
    }

    private static void installApk(Context mContext, long downloadApkId) {
        DownloadManager dManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId);
        if (downloadFileUri != null) {
            //存下文件路径，安装后删除
            String path = downloadFileUri.getPath();
            SharedPreferences.Editor editor = mContext.getSharedPreferences("download", MODE_PRIVATE).edit();
            editor.putLong("apk_id", downloadApkId);
            editor.putString("apk_path", path);
            editor.commit();
            Log.e("DownloadManager", "download :"+downloadApkId);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                downloadFileUri = content://downloads.my-downloads/3
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
            } else {
                intent.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
            }
            mContext.startActivity(intent);

        } else {
            Log.e("DownloadManager", "download error"+downloadApkId);
        }

    }

    private static void installApk(Context mContext, File apkfile) {
        if (apkfile == null || !apkfile.exists()) {
            return;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            contentUri = content://com.dengqi.mymodule.fileprovider/name3/15234234234.apk
            Uri contentUri = FileProvider.getUriForFile(mContext.getApplicationContext(), "com.dengqi.mymodule.fileprovider", apkfile);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
        }
        mContext.startActivity(intent);
    }

}